using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Global : MonoBehaviour
{
    public static Global instance { get; private set; }
    private float timeCount = 0;
    private float level = 1;
    private bool start = false;
    private bool isEnding = false;
    public bool StartFlag
    {
        get
        {
            return start;
        }
    }

    public bool IsEnding
    {
        get
        {
            return isEnding;
        }
    }
    // Start is called before the first frame update
    void Awake()
    {
        instance = this;
        EventManager.GetInstance.StartListening("gameOver", StopGame);
        EventManager.GetInstance.StartListening("happyEnding", HappyEnding);
    }


    private void Start()
    {
        EventManager.GetInstance.TriggerEvent("uiStart");
        
    }

    // Update is called once per frame
    void Update()
    {
        if (start) {
            timeCount += Time.deltaTime;
            if (timeCount >= 80f && !isEnding) {
                Debug.Log("Happy Ending");

                EventManager.GetInstance.TriggerEvent("happyEnding");
            }

            if (timeCount >= 88f && isEnding)
            {
                StopGame();
                EventManager.GetInstance.TriggerEvent("uiOver");
            }
           
        }
        
    }

    public float Level
    {
        get
        {
            return level;
        }
        set
        {
            level = value;
        }
    }
    public float TimeCount
    {
        get
        {
            return timeCount;
        }
        set
        {
            timeCount = value;
        }
    }

    public void StartGame()
    {
        start = true;
        timeCount = 0;
        isEnding = false;
    }

    public void StopGame()
    {
        EventManager.GetInstance.TriggerEvent("uiOver");
        start = false;
        timeCount = 0;
        isEnding = false;
    }

    public void RestartGame()
    {
        Spwaner.instance.resetSpwaner();
        Background.GetInstance.Restart();
        Player.instance.Restart();

        StartGame();
    }

    public void HappyEnding()
    {
        isEnding = true;
        // �����ܶ�����Ļ
        Spwaner.instance.HappyEnding();

        // ����Player�޵�
        Player.instance.SetPlayerMuteki();
    }
}
